#include "fpga-common.h"
#include "bsp/bsp-trk300.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

struct fpga_dev *dev;
int              dac = 4000;

void do_ls_dac()
{
    printf("check LS out\r\n");
    int adc;
    while (1) {
	sleep(1);
	fpga_if_get_gpio(dev, IO_DBM, &adc);
	printf("adc %d\r\n", adc);
    }
}
void do_ls()
{
    int            fd = open("/dev/mxs-hsadc0", O_RDWR);
    unsigned short rbuf[32];

    // printf("set dac %d\r\n", dac);
    // sleep(3);
    // fpga_if_set_gpio(dev, 0, 0);
    // fpga_open_interface("/dev/spidev3.0", &dev, 1000000, "this-otdr-lsopm-32k");
    // fpga_if_set_gpio(dev, 0, 2000);
    // fpga_if_set_gpio(dev, 0, 0);
    // fpga_if_set_gpio(dev, 0, 4000);
    int   dbm_1310, dbm_1550;
    float dbm = -2.0;
    while (1) {
	sleep(2);
	dbm -= 0.1;
	if (dbm < -10) {
	    dbm = 1;
	}

	// fpga_if_set_gpio(dev, IO_MODE, LS_MODE_CW);  // 10 给功率值，float
	// fpga_if_set_gpio(dev, IO_DBM, dbm * 1000);  // 10 给功率值，float
	// printf("dbm1 %f\r\n", dbm);
	// continue;
	int adc = 0;
	fpga_if_get_gpio(dev, 0, &adc);
	fpga_if_get_gpio(dev, 10, &dbm_1310);
	fpga_if_get_gpio(dev, 12, &dbm_1550);
	printf("adc %d dbm 1310:%d dbm 1550:%d\r\n", adc & 0xffff, dbm_1310, dbm_1550);
    }
}

void do_opm()
{
    // int            fd = open("/dev/mxs-hsadc0", O_RDWR);
    // unsigned short rbuf[32];

    printf("set dac %d\r\n", dac);
    int dbm = 0, adc;
    while (1) {
	sleep(1);
	fpga_if_get_gpio(dev, 0, &adc);
	fpga_if_get_gpio(dev, 10, &dbm);
	printf("adc %d dbm %2.2f \r\n", adc, (float)dbm / 100);
	// printf("adc %d dbm %2.2d \r\n", adc, dbm);
    }
}

int main(int argc, char *argv[])
{
    int ls_mode = IO_MODE;

    if (argc == 5 &&
        (strcmp(argv[3], "-m") == 0)) {
	if (strcmp(argv[4], "cw") == 0) {
	    ls_mode = LS_MODE_CW;
	}
	else if (strcmp(argv[4], "off") == 0) {
	    ls_mode = LS_MODE_OFF;
	}
	else if (strcmp(argv[4], "270") == 0) {
	    ls_mode = LS_MODE_270HZ;
	}
	else if (strcmp(argv[4], "1k") == 0) {
	    ls_mode = LS_MODE_1KHZ;
	}
	else if (strcmp(argv[4], "2k") == 0) {
	    ls_mode = LS_MODE_2KHZ;
	}
    }
    if (argc < 3) {
	printf(
		"test OPM 光功\r\n"
		"\ttool-ls.elf -o 1\r\n"
		"test LS 光源\r\n"
		"\ttool-ls.elf -p <dbm>\r\n"
		"\ttool-ls.elf -d <dac>\r\n"
		"\ttool-ls.elf -d <dbm> -m cw/off/270/1k/2k\r\n"
		"\ttool-ls.elf -s 1 单纯发送停止OTDR\r\n");
	printf(
		"例子:\r\n"
		"\ttool-ls.elf -p -3000 输出功率-3dbm\r\n"
		"\ttool-ls.elf -d 4095 -m cw 直流dac输出最大\r\n"
		"\ttool-ls.elf -p -3 输出-3dbm\r\n");
	exit(-1);
    }
    fpga_open_interface("/dev/spidev3.0", &dev, 1000000, "this-otdr-lsopm-32k");

    if (strcmp(argv[1], "-d") == 0) {
	int dac;
	dac = atoi(argv[2]);
	fpga_if_set_gpio(dev, IO_MODE, ls_mode);
	fpga_if_set_gpio(dev, IO_DAC, dac);  // 1 给DAC值，int
	do_ls_dac();
    }
    // tool-ls.elf -p -9.2
    if (strcmp(argv[1], "-p") == 0) {
	int dbm = atoi(argv[2]);
	fpga_if_set_gpio(dev, IO_DBM, dbm);       // 10 给功率值，float
	fpga_if_set_gpio(dev, IO_MODE, ls_mode);  // 10 给功率值，float

	do_ls();
    }
    if (strcmp(argv[1], "-o") == 0) {
	float dbm;
	do_opm();
    }
    if (strcmp(argv[1], "-s") == 0) {
	fpga_otdr_stop(dev);
    }
    if (strcmp(argv[1], "-P") == 0) {
    }
}
